﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Extensions.Options;

using Newtonsoft.Json.Linq;

namespace Utils
{
    public class ConfigService
    {
        private readonly IOptionsMonitor<SecretOptions> optionsMonitor;

        public ConfigService(IOptionsMonitor<SecretOptions> optionsMonitor)
        {
            this.optionsMonitor = optionsMonitor;
        }

        public T GetValue<T>(string name, string key, T defValue)
        {
            return GetValue<T>(name, key) ?? defValue;
        }

        public T GetValue<T>(string name, string key)
        {
            SecretOptions secretOptions = optionsMonitor.Get(name);
            switch (key)
            {
                case "appId":
                    return (T)Convert.ChangeType(secretOptions.AppId, typeof(T));
                case "appSecret":
                    return (T)Convert.ChangeType(secretOptions.AppSecret, typeof(T));
                default:
                    return secretOptions.ExtData[key].Value<T>() ;
            }
        }

        public string GetValue(string name, string key)
        {
            SecretOptions secretOptions = optionsMonitor.Get(name);
            switch (key)
            {
                case "appId":
                    return secretOptions.AppId;
                case "appSecret":
                    return secretOptions.AppSecret;
                default:
                    return secretOptions.ExtData[key]?.ToString();
            }
        }
    }
}
